package com.github.caijh.mall.utils;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.github.caijh.commons.util.DateUtils;
import com.github.caijh.commons.util.KeyValuePairUtils;
import com.github.caijh.commons.util.MD5;
import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.core.exception.BizException;
import com.github.caijh.mall.common.enums.PayType;
import org.apache.commons.lang3.StringUtils;

public class Utils {

    private Utils() {

    }


    /**
     * 获取fin_in_set拼装sql
     *
     * @param field String 字段
     * @param value Integer 值
     * @return String
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    public static String getFindInSetSql(String field, Integer value) {
        return "find_in_set(" + value + ", " + field + ")";
    }

    /**
     * 获取fin_in_set拼装sql
     *
     * @param field String 字段
     * @param list  list
     * @return String
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    public static String getFindInSetSql(String field, List<Integer> list) {
        ArrayList<String> sqlList = new ArrayList<>();
        for (Integer value : list) {
            sqlList.add(getFindInSetSql(field, value));
        }
        return "( " + StringUtils.join(sqlList, " or ") + ")";
    }

    /**
     * 获取fin_in_set拼装sql
     *
     * @param field String 字段
     * @param idStr String 多个值，逗号分割
     * @return String
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    public static String getFindInSetSql(String field, String idStr) {
        List<Integer> list = Strings.toIntListByComma(idStr);
        ArrayList<String> sqlList = new ArrayList<>();
        for (Integer value : list) {
            sqlList.add(getFindInSetSql(field, value));
        }
        return "( " + StringUtils.join(sqlList, " or ") + ")";
    }


    /**
     * 根据长度生成随机数字
     *
     * @param start 起始数字
     * @param end   结束数字
     * @return 生成的随机码
     */
    public static Integer randomCount(Integer start, Integer end) {
        return (new Random().nextInt() * (end - start + 1) + start);
    }

    /**
     * 订单号生成
     *
     * @param payType 支付类型
     * @return 订单号
     */
    public static String getOrderNo(PayType payType) {
        return payType.getOrderPrefix() + DateUtils
                .nowAsString(com.github.caijh.framework.util.constant.Constants.Date.YYYYMMDDHHMMSS) + randomCount(11111, 99999);
    }

    /**
     * unicode编码转换为汉字
     *
     * @param unicodeStr 待转化的编码
     * @return 返回转化后的汉子
     */
    public static String UnicodeToCN(String unicodeStr) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(unicodeStr);
        char ch;
        while (matcher.find()) {
            //group
            String group = matcher.group(2);
            //ch:'李四'
            ch = (char) Integer.parseInt(group, 16);
            //group1
            String group1 = matcher.group(1);
            unicodeStr = unicodeStr.replace(group1, ch + "");
        }

        return unicodeStr.replace("\\", "").trim();
    }

    public static String getSign(Map<String, Object> map, String signKey) {
        Map<String, String> params = new HashMap<>();
        map.forEach((k, v) -> params.put(k, v.toString()));
        String result = KeyValuePairUtils.map2KeyValuePair(params) + "&key=" + signKey;
        try {
            return MD5.INSTANCE.md5(result).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            throw BizException.of("HTTP_500");
        }
    }


    /**
     * 百分比计算
     *
     * @param detailTotalNumber 销售量
     * @param totalNumber       限量库存
     * @return 百分比
     */
    public static int percentInstanceIntVal(Integer detailTotalNumber, Integer totalNumber) {
        Double bfTotalNumber = Double.valueOf(detailTotalNumber);
        Double zcTotalNumber = Double.valueOf(totalNumber);
        double percent = bfTotalNumber / zcTotalNumber;
        double pec = percent * 100;
        return (int) pec;
    }

}
